<?php
/*
 * 合同
*/
class Contract_model extends CI_Model {
	public function __construct() {
		parent :: __construct();
	}

	public function count_nums(){ /*取得总行数*/
		$user_id = $this->session->userdata('id');
		$role = $this->session->userdata('role');
		if($role == '市场'){
			$this->db->where('owner_id',$user_id)->from('contract');
			$count = $this->db->count_all_results();
		}else{
			$count = $this->db->count_all('contract');
		}
		return $count;
	}

	public function list_contracters($limit,$offset = 0){ /*获取分页contacter数据*/
		$user_id = $this->session->userdata('id');
		$role = $this->session->userdata('role');
		$contracts = array();
		if($role == '市场'){
			$query = $this->db->where(owner_id,$user_id)->from('contract')->order_by("create_time", "desc")->limit($limit, $offset)->get();
		}else{
			$query = $this->db->from('contract')->order_by("create_time", "desc")->limit($limit, $offset)->get(); 
		}
		foreach($query->result() as $row) {
			$customer_query = $this->db->get_where ( 'customer',array ('id' => $row->customer_id), $condition_array );
			$customer = $customer_query->row();
			$row->customer_name = $customer->customer_name;

			$user_query = $this->db->get_where ( 'user', array ('id' => $row->owner_id ) );
			$user = $user_query->row();
			$row->user_name = $user->user_name;

			$contracts[] = $row;
		}
		return $contracts;
	}
}
?>
